﻿using __Able.Network.Packets;

namespace __Able.Network.Threading
{
    /// <summary>
    /// 包括处理触发器
    /// </summary>
    public class PacketIncomeTrigger : ICallable
    {
        private Channel channel;
        public PacketIncomeTrigger(Channel channel)
        {
            this.channel = channel;
        }
        /// <summary>
        /// 
        /// </summary>
        public bool IsRunning
        {
            get;
            private set;
        }
        /// <summary>
        /// WaitCallback回调
        /// </summary>
        /// <param name="state">The state.</param>
        public void Call(object state)
        {
            if (!IsRunning)
            {
                lock (this)
                {
                    if (!IsRunning)
                    {
                        IsRunning = true;
                        _InPacket inPacket = channel.PacketManager.RemoveIncomingPacket();
                        while (inPacket != null)
                        {
                            channel.PacketManager.FirePacketArrivedEvent(inPacket);
                            inPacket = channel.PacketManager.RemoveIncomingPacket();
                        }
                        IsRunning = false;
                    }
                }
            }
        }
    }
}
